package com.fuershiye.woman.monitor.controller;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fuershiye.woman.monitor.beans.IdNamePid;
import com.fuershiye.woman.monitor.beans.IdNameSname;
import com.fuershiye.woman.monitor.model.Department;
import com.fuershiye.woman.monitor.model.User;
import com.fuershiye.woman.monitor.service.AdminService;
import com.fuershiye.woman.monitor.service.AreaService;
import com.fuershiye.woman.monitor.service.DepartmentService;
import com.fuershiye.woman.monitor.service.DepartmentTypeService;
import com.fuershiye.woman.monitor.shiro.credentials.PasswordHelper;

/**
 * 传统区域的管理
 * 
 * @author yongyongdong
 *
 */
@Controller
@RequestMapping(value = "/personalCenter")
public class PersonalCenterController {

    @Autowired
    AdminService adminService;
    @Autowired
    DepartmentService departmentService;
    @Autowired
    DepartmentTypeService departmentTypeService;
    @Autowired
    AreaService areaService;

    @RequestMapping(value="/view.do")
    public String view(String show){
    	
        return "personalCenter/" + show;
        
    }

    @RequestMapping(value = "/viewDepartment.do")
    public String viewDepartment(Model model) {

        List<IdNamePid> departList = departmentTypeService.getAllNames();
        model.addAttribute("departList", departList);

        List<IdNameSname> pidDepartList = departmentService.getAllSnames(null,null,null);
        model.addAttribute("pidDepartList", pidDepartList);

        List<IdNamePid> areaList = areaService.getAllNames();
        model.addAttribute("areaList", areaList);

        Department department = departmentService.findByUserName((String)SecurityUtils.getSubject().getPrincipal());
        model.addAttribute("department", department);
        
        return "personalCenter/view";
    }


	@RequestMapping(value = "/modify.do")
	@ResponseBody
	public boolean modifyPass(String newPass) {
        String userName = (String) SecurityUtils.getSubject().getPrincipal();
        User user= adminService.findByName(userName);
        String password = PasswordHelper.encryptPassword(userName,newPass);
        user.setPassword(password);
        adminService.save(user);
		return true;
	}


}
